<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
session_start();
require_once 'code/Tasks.php';
require_once 'code/Util.php';
?>
<html>
    <head>
        <link rel="shortcut icon" href="images/iTrack.ico"> 
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="PRIVATE">
        <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="en-US">
        <META NAME="DESCRIPTION" CONTENT="Tasks application home page">
        <META NAME="KEYWORDS" CONTENT="iTrack">
        <META NAME="COPYRIGHT" CONTENT="&copy; iTrack 2013">

        <title>iTrack - Create Task</title>

        <link href="assets/css/bootstrap.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-top: 60px;
            }
        </style>
        <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
        <script src="assets/js/jquery.js"></script>        
        <script type="text/javascript" src="jquery/jquery-ui-1.8.13.custom.min.js"></script>


        <script type="text/javascript">
            $(document).ready(function() {
                $("#txtTask").focus();
            });
            
            function saveTask() {
                var txtTask = document.getElementById("txtTask");
                if(txtTask.value != "") {
                    document.getElementById("frm").submit();
                } else {
                    alert('Cannot save task without description !');
                    document.getElementById("txtTask").focus();
                    return false;
                }            
            }
            
            function CreateProjectPopup() {
                document.getElementById("txtProjectName").focus();
                $('#myModal').modal('show');
            }
		
            function SaveProject() {
                var projectid = $("#projectid").val();
                var projectname = $("#txtProjectName").val();
                var projectdesc = $("#txtProjectDescription").val();
                var visibility = $("input:radio[name=Visibility]:checked").val();
		
                if(projectname != "") {
                    $.ajax({         
                        url: "../Controller", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "saveproject",
                            "userid": $("#userid").val(),
                            "projectid":projectid,
                            "projectname" : projectname,
                            "description" : projectdesc,
                            "visibility"  : visibility
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    window.location ="viewproject.php?projectid="+projectid;
                                } else {
                                    alert("Error saving project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location = "error.php";
                        }
                    });
                }
                return false;
            }
            
            function DeleteProject() {
                if(confirm("Deleting Project. Are you sure?") == true) {
                    $.ajax({         
                        url: "../Controller", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "deleteproject",
                            "userid": $("#userid").val(),
                            "projectid":$("#projectid").val()
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    //window.location ="projects.php" + "?random="+new Date().getTime();
                                } else {
                                    alert("Error deleting project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location ="projects.php" + "?random="+new Date().getTime();
                        }
                    });
                }
		
                return false;
            }
        </script>
        <style type="text/css">
            .sidebar-nav {
                padding: 9px 0;
            }
            .ckeditor {
                width: 100%;
                margin: 5px 0;
                padding: 3px;
                -webkit-box-sizing: border-box;
                -moz-box-sizing: border-box;
                box-sizing: border-box;
            }
        </style>    
    </head>
    <body>
        <?php
        if (isset($_SESSION['userid'])) {
            $userid = $_SESSION['userid'];
            $projectid = $_GET['projectid'];
            ?>

            <?php
            Util::getNavBar();
            ?>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="span2">
                        <div class="well sidebar-nav">
                            <?php
                            echo Util::getLeftNavList('Projects');
                            ?>
                        </div><!--/.well -->
                    </div><!--/span-->
                    <div class="span10">          
                        <div class="row-fluid">
                            <div class="span12">
                                <?php
                                $tasksObj = new Tasks();
                                $projects = array();

                                if ($projectid > 0) {
                                    $projects = $tasksObj->getAllProjects($userid, $projectid);
                                }

                                $projectname = "N/A";
                                $description = "N/A";
                                //echo count($projects);
                                if (count($projects) > 0) {
                                    foreach ($projects as $db_field) {
                                        $projectname = $db_field['project'];
                                        $description = $db_field['description'];
                                    }
                                    ?>
                                    <h2><a href="viewproject.php?projectid=<?php echo $projectid ?>"><?php echo $projectname ?></a>
                                        &nbsp;&nbsp;<a class="btn btn-mini" type="button" href="javascript:void(0);" onclick="CreateProjectPopup()"><i class="icon icon-edit"></i>&nbsp; Edit</a>
                                    </h2>
                                    <p class="muted"><?php echo $description ?></p>

                                    <h3>Create Task</h3>
                                    <form id="frm" name="frm" action="savetask.php" method="POST" >
                                        <input type="hidden" id="userid" name="userid" value="<?php echo $_SESSION['userid']; ?>">
                                        <input type="hidden" id="projectid" name="projectid" value="<?php echo $_GET['projectid']; ?>">
                                        <input type="hidden" id="operation" name="operation" value="insert">

                                        <div class="well listcontents">
                                            <div class="control-group">            
                                                <div class="controls">
                                                    <label class="control-label" for="txtTask"><abbr title="Task Description">Description</abbr></label>
                                                    <div class="controls">
                                                        <textarea class="ckeditor" id="txtTask" name="txtTask" rows="5"  tabindex="1"></textarea>				              
                                                    </div>				            		            
                                                </div>						
                                            </div>
                                        </div> 

                                        <table class="table-condensed">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        <div class="control-group">
                                                            <label class="control-label heading" for="selType"><abbr title="Task Type">Type</abbr></label>
                                                            <div class="controls">
                                                                <?php
                                                                Util::getTaskTypeSelBox('selType', 'selType', '');
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <div class="control-group">
                                                            <label class="control-label heading" for="selStatus"><abbr title="Task Status">Status</abbr></label>
                                                            <div class="controls">
                                                                <?php
                                                                Util::getTaskStatusSelBox('selStatus', 'selStatus', '');
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>

                                        <div class="control-group" style="padding-top: 10px;">
                                            <div class="controls">
                                                <button type="button" class="btn btn-primary" onclick="saveTask()"><i class="icon-envelope"></i>&nbsp;&nbsp; Save &nbsp;</button>
                                                <a class="btn btn-link" href="viewproject.php?projectid=<?php echo $projectid ?>"><i class="icon-plus-sign"></i>&nbsp;&nbsp; Cancel &nbsp;</a>
                                            </div>
                                        </div>
                                    </form>

                                    <?php
                                }
                                ?>

                            </div><!--/span-->          
                        </div><!--/row-->

                    </div><!--/span-->
                </div><!--/row-->

                <!-- Include footer -->
                <?php include_once 'footer.php'; ?>
            </div>

            <!-- Dialogs -->
            <div class="modal hide fade in" id="myModal" tabindex="-1"
                 role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
                 style="width: 25em">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h3 id="myModalLabel"><i class="icon-bullhorn"></i>&nbsp;Edit Project</h3>
                </div>
                <div class="modal-body">
                    <div class="control-group">
                        <div class="controls">
                            <label class="control-label" for="txtProjectName"><abbr
                                    title="Project Name">Project Name</abbr></label>
                            <div class="controls">
                                <input type="text" class="input-xlarge" id="txtProjectName"
                                       name="txtProjectName" autofocus value="<?php echo $projectname ?>">
                            </div>
                        </div>
                        <div class="controls">
                            <label class="control-label" for="txtProjectDescription"><abbr
                                    title="Project Description">Description</abbr></label>
                            <div class="controls">
                                <textarea class="input-xlarge" id="txtProjectDescription"
                                          name="txtProjectDescription" rows="5"><?php echo $description ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-danger" onclick="DeleteProject()">Delete</button>
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-primary" onclick="SaveProject()">Save</button>
                </div>
            </div>

            <!-- Placed at the end of the document so the pages load faster -->            
            <script src="assets/js/bootstrap-transition.js"></script>
            <script src="assets/js/bootstrap-alert.js"></script>
            <script src="assets/js/bootstrap-modal.js"></script>
            <script src="assets/js/bootstrap-dropdown.js"></script>
            <script src="assets/js/bootstrap-scrollspy.js"></script>
            <script src="assets/js/bootstrap-tab.js"></script>
            <script src="assets/js/bootstrap-tooltip.js"></script>
            <script src="assets/js/bootstrap-popover.js"></script>
            <script src="assets/js/bootstrap-button.js"></script>
            <script src="assets/js/bootstrap-collapse.js"></script>
            <script src="assets/js/bootstrap-carousel.js"></script>
            <script src="assets/js/bootstrap-typeahead.js"></script>

            <?php
        } else {
            header("location: login.php");
        }
        ?> 
    </body>
</html>